package com.wd.shared.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.GZIPOutputStream;

public class GZWriter {
    public String fileName;
    private BufferedWriter out = null;

    public GZWriter(String fname) {
        fileName = fname;
    }

    public boolean open() {
        try {
            if (fileName.endsWith(".gz")) {
                out = new BufferedWriter(new OutputStreamWriter(
                        new GZIPOutputStream(new FileOutputStream(fileName))));
            } else {
                out = new BufferedWriter(new FileWriter(fileName));
            }
        } catch (IOException ex) {
            System.err.println("Error: cannot create file: " + fileName);
            return false;
        }
        return true;
    }

    public boolean close() {
        try {
            out.close();
            return true;
        } catch (IOException ex) {
            System.err.println("Error: Failed to close file: " + fileName);
            return false;
        }
    }

    // puts a line. returns false on failure
    public boolean putLine(String line) {
        if (line.indexOf("\n") < 0) {
            line = line + "\n";
        }
        try {
            out.write(line, 0, line.length());
            out.flush();
            return true;
        } catch (IOException ex) {
            System.err.println("Error: Failed to write to file: " + fileName);
            return false;
        }
    }

    public static void main(String args[]) {
        String path = "E:\\work\\project_J\\Test\\test\\0407_new.txt.gz";
        GZWriter gzw = new GZWriter(path);
        gzw.open();

        List<String> list = readLine("E:\\work\\project_J\\Test\\test\\phase2B_T_70_20080325_20080506_20080407.txt\\a.txt");
        for (String str : list) {
            gzw.putLine(str);
        }
        if (list != null) {
            System.out.println("Total write line : " + list.size());
        }
        gzw.close();
    }

    public static List<String> readLine(String path) {

        FileReader fr = null;
        BufferedReader br = null;
        List<String> list = new LinkedList<String>();
        String str = "";
        try {
            fr = new FileReader(path);
            br = new BufferedReader(fr);
            while ((str = br.readLine()) != null) {
                list.add(str + "\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                fr.close();
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

}
